<!DOCTYPE html>
<!-- saved from url=(0044)http://www.cplusplus.com/forum/unices/36461/ -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Linux Console Color (the "\033[" way) - C++ Forum</title>
<link rel="shortcut icon" type="image/x-icon" href="http://www.cplusplus.com/favicon.ico">
<link rel="stylesheet" type="text/css" href="./Linux Console Color - C++ Forum_files/main.css">
<script type="text/javascript" async="" src="./Linux Console Color - C++ Forum_files/ga.js"></script><script src="./Linux Console Color - C++ Forum_files/main.js" type="text/javascript"></script>
</head>
<body>
<div id="I_top">
<div id="I_header">
<div id="I_logo"><a href="http://www.cplusplus.com/" title="cplusplus.com"><div></div></a></div>
<div id="I_search">
<form id="search" action="http://www.cplusplus.com/search.do" method="get">
Search: <input name="q" size="20" class="txt"> <input type="submit" value="Go" class="btn">
</form>
</div>
<div id="I_bar">
<ul>
<li><a href="http://www.cplusplus.com/forum/">Forum</a></li>
<li><a href="http://www.cplusplus.com/forum/unices/">UNIX/Linux Programming</a></li>
<li class="here">Linux Console Color (the "\033[" way)</li>
</ul>
</div>
<div id="I_user" class="C_LoginBox"><span title="ajax"></span><div id="I_corner" style="display: block; "><div id="I_cornerinner"><div id="I_userid">Not logged in</div></div></div><div id="I_userbuttons"><a href="http://www.cplusplus.com/user/access.cgi?w=signup">register</a><a id="I_btnlogin" href="javascript:LBI_user.modal(1)">log in</a></div><div id="I_fold" style="display: none; "><div class="deco"></div><form name="I_loginform" id="I_loginform" action="http://www.cplusplus.com/user/access.cgi" onsubmit="LBI_user.login(); return false;" method="post" style="display: none; "><div class="fields"><input type="hidden" name="w" value="login"><input type="hidden" name="y" value="1"><input type="hidden" name="to" value="http://www.cplusplus.com/forum/unices/36461/">user name: <input class="txt" name="l" size="8" type="text" title="Type user name"><br>password: <input class="txt" name="p" size="8" type="password" title="Type password"><br><input class="chk" name="rememberme" id="rememberme" value="1" type="checkbox"><label for="rememberme">remember me</label>&nbsp;<input class="btn" value="sign in" type="submit"><br><div class="forgot"><a href="http://www.cplusplus.com/user/access.cgi?w=lostpass">forgot your password?</a></div></div><div class="openid">or sign in using:<br><a class="google" href="http://www.cplusplus.com/user/access.cgi?w=login&y=1&pr=google&to=%2Fforum%2Funices%2F36461%2F" title="Sign in using your google account"><span></span></a><a class="yahoo" href="http://www.cplusplus.com/user/access.cgi?w=login&y=1&pr=yahoo&to=%2Fforum%2Funices%2F36461%2F" title="Sign in using your Yahoo! account"><span></span></a> </div></form><div id="I_loginwait" style="display: none; "></div><div id="I_loginfail" style="display: none; "><div id="I_loginfailtext"></div><a class="C_bRepeat" href="javascript:LBI_user.modal(1)">try again</a><a class="C_bCancel" href="javascript:LBI_user.modal(0)">cancel</a><div class="forgot"><a href="http://www.cplusplus.com/user/access.cgi?w=lostpass">forgot your password?</a></div></div><div class="close"><a href="javascript:LBI_user.modal(0)" title="cancel log in"><span></span></a></div></div></div></div></div>
<div id="I_mid">
<div id="I_wrap">
<div id="I_minheight" style="height: 479px; "></div>
<div id="I_main">
<div class="C_ad728">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-1947171228411990";
google_ad_slot = "5020710848";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript" src="./Linux Console Color - C++ Forum_files/show_ads.js">
</script><ins style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><ins id="aswift_0_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><iframe allowtransparency="true" frameborder="0" height="90" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){w.location.replace(h)}}" scrolling="no" vspace="0" width="728" id="aswift_0" name="aswift_0" style="left:0;position:absolute;top:0;"></iframe></ins></ins>
</div><div id="I_content"><h3><div class="C_ico default" title="post">&nbsp;</div> Linux Console Color (the "\033[" way)</h3><span id="CH_edttl"></span><span class="rootdatPost" title="36461,root,0,-1,2,0"></span><div id="CH_PostList"><div class="C_forPost" id="msg197219"><span title="197219,51911,255,1,0"></span><table class="box">
<tbody><tr class="rtop">
<td class="dwho"><a href="http://www.cplusplus.com/user/ToRcH/"><b>ToRcH</b> (1)</a></td>
<td class="dwhen"><a href="http://www.cplusplus.com/forum/unices/36461/#msg197219" title="Link to this post"><img src="./Linux Console Color - C++ Forum_files/link.png" width="16" height="8"></a> <span id="CH_zTm5d6f16b798" title="Tue, 15 Feb 2011 11:14:05 +0000">Feb 15, 2011 at 12:14pm</span><script type="text/javascript">WhenId('CH_zTm5d6f16b798');</script><noscript>Feb 15, 2011 at 11:14am UTC</noscript></td>
</tr>
<tr><td class="dwhat" colspan="2" id="CH_i197219">
Hi all.<br>
<br>
I just wanted to help people in this matter since I too went through this and had some problems with good answers related to this problem.<br>
<br>
(and I also wanted to help Duoas for I've seen him struggling to tell people to stop using "system" calls. Good Job Duoas)<br>
<br>
This is a list of codes used in C++ to change the text color:<br>
<br>
black - 30<br>
red - 31<br>
green - 32<br>
brown - 33<br>
blue - 34<br>
magenta - 35<br>
cyan - 36<br>
lightgray - 37<br>
<br>
Now these are the basic colours.<br>
<br>
Usage of "\033[":<br>
This is to handle the console cursor. I do not have a complete reference so I ask people who know about it to comment with what I do not have.<br>
<br>
* 'm' character at the end of each of the following sentences is used as a stop character, where the system should stop and parse the \033[ sintax.<br>
<br>
\033[0m - is the default color for the console<br>
\033[0;#m - is the color of the text, where # is one of the codes mentioned above<br>
\033[1m - makes text bold<br>
\033[1;#m - makes colored text bold**<br>
\033[2;#m - colors text according to # but a bit darker<br>
\033[4;#m - colors text in # and underlines<br>
\033[7;#m - colors the background according to #<br>
\033[9;#m - colors text and strikes it<br>
\033[A - moves cursor one line above (carfull: it does not erase the previously written line)<br>
\033[B - moves cursor one line under<br>
\033[C - moves cursor one spacing to the right<br>
\033[D - moves cursor one spacing to the left<br>
\033[E - don't know yet<br>
\033[F - don't know yet<br>
<br>
\033[2K - erases everything written on line before this.<br>
<br>
**(this could be usefull for those who want more colors. The efect given by bold will give a color change effect)<br>
<br>
As a short and simple example that colors text and the bold version:<br>
<br>
<div class="auto"><table class="snippet"><tbody><tr><td class="rownum"><pre><code>1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br></code></pre></td>
<td class="source"><pre><code><dfn>#include &lt;iostream&gt;</dfn>
<dfn>#include &lt;string&gt;</dfn>

<var>int</var> main(<var>int</var> argc, <var>char</var> ** argv)
{
    std::string default_console = <kbd>"\033["</kbd>+itoa(0)+<kbd>"m"</kbd>;
    <var>for</var> (<var>int</var> i = 30; i &lt;= 37; i++)
    {
        std::string color = <kbd>"\033[0;"</kbd>+itoa(i)+<kbd>"m"</kbd>;

        std::cout&lt;&lt;color&lt;&lt;<kbd>"test "</kbd>&lt;&lt;i&lt;&lt;std::endl;

        color = <kbd>"\033[1"</kbd>+itoa(i)+<kbd>"m"</kbd>;

        std::cout&lt;&lt;color&lt;&lt;<kbd>"test "</kbd>&lt;&lt;i&lt;&lt;<kbd>" bold"</kbd>&lt;&lt;std::endl;
        std::cout&lt;&lt;default_console&lt;&lt;std::endl;

    }

    std::cout &lt;&lt; <kbd>"Done"</kbd> &lt;&lt; std::endl;

    <var>return</var> 0;
}</code></pre></td></tr></tbody></table></div>
<br>
<br>
Hope this is usefull,<br>
ToRcH<br>
<br>
P.S.: If anyone knows other special characters please comment and add them. TY.
</td></tr>
<tr>
<td colspan="2" class="dhow">
<div class="dedit">
<span class="dbtn" id="CH_btn197219"></span>

</div>
</td>
</tr>
</tbody></table>
</div>
<div class="C_forPost" id="msg197255"><span title="197255,17129,1023,5440,0"></span><table class="box">
<tbody><tr class="rtop">
<td class="dwho"><a href="http://www.cplusplus.com/user/kbw/"><b>kbw</b> (5440)</a></td>
<td class="dwhen"><a href="http://www.cplusplus.com/forum/unices/36461/#msg197255" title="Link to this post"><img src="./Linux Console Color - C++ Forum_files/link.png" width="16" height="8"></a> <span id="CH_zTm253f154ff1" title="Tue, 15 Feb 2011 13:49:01 +0000">Feb 15, 2011 at 2:49pm</span><script type="text/javascript">WhenId('CH_zTm253f154ff1');</script><noscript>Feb 15, 2011 at 1:49pm UTC</noscript></td>
</tr>
<tr><td class="dwhat" colspan="2" id="CH_i197255">
<a href="http://ascii-table.com/ansi-escape-sequences.php">http://ascii-table.com/ansi-escape-sequences.php</a><br>
<a href="http://ascii-table.com/ansi-escape-sequences-vt-100.php">http://ascii-table.com/ansi-escape-sequences-vt-100.php</a>
</td></tr>
<tr>
<td colspan="2" class="dhow">
<div class="dedit">
<span class="dbtn" id="CH_btn197255"></span>

</div>
</td>
</tr>
</tbody></table>
</div>
</div><div class="rootinsMore"></div><div class="rootbtnMore"></div><div class="rootinsNew"></div><div class="rootbtnNew"></div><div id="CH_insNew"></div><div id="CH_reply">Topic archived. No new replies allowed.</div><div id="CH_subscription"></div><div class="rootedtNew"></div><script type="text/javascript">new for_PostList(36461,1,51911,0,'CH_PostList','CH_subscription','CH_reply','CH_insNew','CH_edttl','/forum/thread.cgi','/forum/post.cgi','/forum/myposts.cgi',64,32,512,256,1024,16);</script></div></div><div id="I_nav">
<div class="sect root">
<h3><b><a href="http://www.cplusplus.com/">C++</a></b></h3>
<ul>
<li class="folder info"><a href="http://www.cplusplus.com/info/">Information</a></li>
<li class="folder doc"><a href="http://www.cplusplus.com/doc/">Documentation</a></li>
<li class="folder reference"><a href="http://www.cplusplus.com/reference/">Reference</a></li>
<li class="folder articles"><a href="http://www.cplusplus.com/articles/">Articles</a></li>
<li class="folder selected forum"><a href="http://www.cplusplus.com/forum/">Forum</a></li>
</ul>
</div>
<div class="sect">
<h3><b><a href="http://www.cplusplus.com/forum/">Forum</a></b></h3>
<ul>
<li><a href="http://www.cplusplus.com/forum/beginner/"><b>Beginners</b></a></li><li><a href="http://www.cplusplus.com/forum/windows/"><b>Windows Programming</b></a></li><li class="selected"><a href="http://www.cplusplus.com/forum/unices/"><b>UNIX/Linux Programming</b></a></li><li><a href="http://www.cplusplus.com/forum/general/"><b>General C++ Programming</b></a></li><li><a href="http://www.cplusplus.com/forum/lounge/"><b>Lounge</b></a></li><li><a href="http://www.cplusplus.com/forum/jobs/"><b>Jobs</b></a></li></ul>
</div>
<div id="I_subnav"></div>
<div class="C_ad234">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-1947171228411990";
google_ad_slot = "7556847525";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript" src="./Linux Console Color - C++ Forum_files/show_ads.js">
</script><ins style="display:inline-table;border:none;height:60px;margin:0;padding:0;position:relative;visibility:visible;width:234px"><ins id="aswift_1_anchor" style="display:block;border:none;height:60px;margin:0;padding:0;position:relative;visibility:visible;width:234px"><iframe allowtransparency="true" frameborder="0" height="60" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){w.location.replace(h)}}" scrolling="no" vspace="0" width="234" id="aswift_1" name="aswift_1" style="left:0;position:absolute;top:0;"></iframe></ins></ins>
</div></div>
<div id="I_midclear"></div>
</div></div>
<div id="I_bottom">
<div id="I_footer">
	<a href="http://www.cplusplus.com/">Home page</a> | <a href="http://www.cplusplus.com/privacy.do">Privacy policy</a><br>© cplusplus.com, 2000-2013 - All rights reserved - <i>v3.1</i><br><a href="http://www.cplusplus.com/contact.do?referrer=http%3A%2F%2Fwww.cplusplus.com%2Fforum%2Funices%2F36461%2F">Spotted an error? contact us</a>
</div>
</div>


<script type="text/javascript">
<!--
function NavFor(us) {document.getElementById('I_subnav').innerHTML=us.ok?'<div class="sect"><h3><b><a href="/user/">'+us.user+'</a></b></h3><ul><li><a href="/forum/myposts.cgi">My topics</a></li></ul></div>':'';}onSession(NavFor);ready();
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-521783-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

//-->
</script>


</body></html>